package com.chat.view;

import com.chat.FileAddress;
import com.chat.Local;
import com.chat.common.ChatMsg;
import com.chat.common.Message;
import com.chat.common.MessageType;
import com.chat.io.SetFile;
import com.chat.service.ClientConnectServerThread;
import com.chat.service.ManageClientConnectServerThread;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.text.Text;

import java.awt.*;
import java.io.File;
import java.io.IOException;

public class ChatLeftFileController {
    public ImageView touxiang;
    public Text nickname;
    public Text filename;
    public String address;
    public String userid;
    public byte[] file;//文件的二进制流
    public Text time;
    public Button receivebutton;

    public void setAddress(String address) {
        this.address = address;
    }

    public void setTouxiang(String username) {
        File file1=new File("D:/chatproject/client/"+username+".png");
        this.touxiang.setImage(new Image("file:"+file1.getAbsolutePath()));
    }

    public void setNickname(String nickname) {
        this.nickname.setText(nickname);
    }

    public void setTime(String time) {
        this.time.setText(time);
    }

    public void setFilename(String filename) {
        this.filename.setText(filename);
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public void setFile(byte[] file) {
        this.file = file;
    }

    public void openfile(){
        File file1=new File(FileAddress.LOCAL_WARHOUSE+address);
        if(file1.exists()){
            System.out.println("文件存在，直接从本地获取打开文件");
            Desktop desktop = Desktop.getDesktop();
            try {
                desktop.open(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            System.out.println("文件不存在，从服务端下载文件");
            File file=download();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Desktop desktop = Desktop.getDesktop();
            if (file.exists()) {
                try {
                    desktop.open(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            receivebutton.setText("打开文件");
        }
    }
    public File download(){
          Message msg=new Message();
          msg.setMesType(MessageType.MESSAGE_DOWNLOAD_FILE);
        ChatMsg cm=new ChatMsg();
        cm.setAddress(address);
        cm.setSenderid(Local.user.getUsername());
        cm.setFilename(filename.getText());
        msg.setData(cm);
        ClientConnectServerThread clientConnectServerThread= ManageClientConnectServerThread.getClientConnectServerThread(Local.user.getUsername());
        clientConnectServerThread.send(msg);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        File file=new File(FileAddress.LOCAL_WARHOUSE+address);
        return file;
    }
}
